package org.drjava.config.db;/**
 * Created by jiangchen on 2017/7/19.
 */

import javax.sql.DataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

/**
 * @author 陈江
 * @version 1.0
 * @date 2017/7/19
 */
@Configuration
@MapperScan(basePackages = "org.drjava.dao.mapper.slave",sqlSessionFactoryRef = "slaveSqlSessionFactory")
public class SlaveSourceConfig {
  @Bean(name = "slaveDataSource")
  @ConfigurationProperties("spring.datasource.slave")
  public DataSource slaveDataSource(){
    return DataSourceBuilder.create().build();
  }

  @Bean(name = "slaveSqlSessionFactory")
  public SqlSessionFactory sqlSessionFactory(@Qualifier("slaveDataSource") DataSource dataSource) throws Exception {
    SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();
    sessionFactoryBean.setDataSource(dataSource);
    sessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver()
        .getResources("classpath*:mapper/slave/*.xml"));
    return sessionFactoryBean.getObject();
  }
}